
const {MYSQL}  = require("../../../../../../../../ruoling-common/src/com/ruoling/common/config/mysqlconfig.js");

const md5 = require("md5-node");

module.exports = class LoginService{

    /**
     * 用户登录
     * @param sys_loginname
     * @param sys_loginpwd
     * @param captchaImage
     * @param session
     * @param callback
     * @constructor
     */
    static async UserLogin(sys_loginname,sys_loginpwd,captchaImage,session,Request,ip,browser,OS,address,callback){
        /**
         * 1、判断 验证码，验证码失败插入日志
         * 2、判断 登录失败插入日志。
         *
         */
        let startTrans = null;
        try{
            const connect = await MYSQL().connect();
            startTrans = await connect.startTrans();

            if(captchaImage == session.captchaImage){
                //验证码成功
                let obj = {
                    sql:"SELECT * FROM rl_sys_user WHERE sys_loginname = \""+sys_loginname+"\" and sys_loginpwd = \""+md5(sys_loginpwd)+"\" and Is_delete = 1",
                };
                let sys_user = await startTrans.query(obj.sql);

                //判断用户与密码
                if(sys_user.length == 1){

                    if(sys_user[0].sys_user_state == 1){
                        //正常

                        let obj = {
                            sql:"insert into rl_sys_login_info(sys_login_user_name,sys_login_ipaddr,sys_login_address,sys_login_browser,sys_login_os,sys_login_state,sys_login_msg,sys_login_time,Create_time)values(?,?,?,?,?,?,?,?,?)",
                            param:[sys_loginname,ip,address,browser,OS,1,"登录成功",new Date(),new Date()]
                        };
                        let sys_user_obj = {
                            sql:"update rl_sys_user set sys_endtime = ?,sys_login_ip= ? where ID = "+sys_user[0].ID+"",
                            param:[new Date(),ip]
                        }
                        await startTrans.query(sys_user_obj.sql,sys_user_obj.param);
                        await startTrans.query(obj.sql,obj.param);


                        //插入用户状态数据
                        //判断当前数据库是否存在sessionId一直的用户
                        let sys_user_onlineobj = {
                            sql:"SELECT * FROM rl_sys_user_online WHERE sys_user_sessionid = \""+Request.sessionID+"\" AND Is_delete = 1"
                        }
                        let sys_user_online_data = await startTrans.query(sys_user_onlineobj.sql);

                        let sys_user_online = {
                            sql:"insert into rl_sys_user_online(sys_user_sessionid,sys_user_login_name,sys_user_login_ipaddr,sys_user_login_address,sys_user_login_browser,sys_user_login_os,sys_user_login_state,sys_start_time_stamp,sys_last_access_time,sys_expire_time,Create_time)values(?,?,?,?,?,?,?,?,?,?,?)",
                            param:[Request.sessionID,sys_user[0].sys_loginname,ip,address,browser,OS,"on_line",new Date(),new Date(),1800,new Date()]
                        }
                        //当他登录之后，直接跳转页面再次登录
                        if(sys_user_online_data.length == 1){
                            let sys_user_off_line = {
                                sql:"delete from rl_sys_user_online where sys_user_sessionid = \""+sys_user_online_data[0].sys_user_sessionid+"\" AND Is_delete = 1 "
                            };
                            await startTrans.query(sys_user_off_line.sql);
                        }
                            await startTrans.query(sys_user_online.sql,sys_user_online.param);
                        //菜单数据
                        //超级管理员
                        if(sys_user[0].sys_admin_type == 1){

                            let menu_obj = {
                                sql:"SELECT * FROM rl_sys_menu_permisson WHERE sys_available = 1 AND Is_delete = 1 AND sys_parent_id <> 0"
                            };
                            let menu_list =  await startTrans.query(menu_obj.sql);
                            startTrans.commit();
                            callback(null,{code:0,msg:"登录成功",data:{user:sys_user[0],menu:menu_list}});
                            return;

                        }else{

                            let menu_obj = {
                                sql:"SELECT distinct srm.sys_menu_id as ID,smp.sys_permission_url,smp.sys_parent_id,smp.sys_menu_type,smp.sys_menu_icon,smp.sys_menu_name,smp.sys_type,smp.Is_delete  FROM rl_sys_user_role as sur LEFT JOIN rl_sys_role_menu as srm ON sur.sys_role_id = srm.sys_role_id LEFT JOIN rl_sys_menu_permisson as smp ON srm.sys_menu_id = smp.ID WHERE sur.sys_user_id = "+sys_user[0].ID+" AND sur.Is_delete = 1 AND srm.Is_delete = 1 AND smp.Is_delete = 1 AND smp.sys_parent_id <> 0"
                            };
                            let menu_list =  await startTrans.query(menu_obj.sql);
                            startTrans.commit();
                            callback(null,{code:0,msg:"登录成功",data:{user:sys_user[0],menu:menu_list}});
                            return;
                        }



                    }else{
                        //已停用
                        let obj = {
                            sql:"insert into rl_sys_login_info(sys_login_user_name,sys_login_ipaddr,sys_login_address,sys_login_browser,sys_login_os,sys_login_state,sys_login_msg,sys_login_time,Create_time)values(?,?,?,?,?,?,?,?,?)",
                            param:[sys_loginname,ip,address,browser,OS,0,"账户已停用",new Date(),new Date()]
                        };
                        startTrans.query(obj.sql,obj.param);
                        startTrans.commit();
                        callback(null,{code:4,msg:"账户已停用"});
                        return;
                    }
                }else{
                    //用户名或密码
                    let obj = {
                        sql:"insert into rl_sys_login_info(sys_login_user_name,sys_login_ipaddr,sys_login_address,sys_login_browser,sys_login_os,sys_login_state,sys_login_msg,sys_login_time,Create_time)values(?,?,?,?,?,?,?,?,?)",
                        param:[sys_loginname,ip,address,browser,OS,0,"用户名或密码不正确",new Date(),new Date()]
                    };
                    startTrans.query(obj.sql,obj.param);
                    startTrans.commit();
                    callback(null,{code:3,msg:"用户名或密码"});
                    return;
                }

            }else{
                //验证码验证失败
                let obj = {
                    sql:"INSERT INTO rl_sys_login_info(sys_login_user_name,sys_login_ipaddr,sys_login_address,sys_login_browser,sys_login_os,sys_login_state,sys_login_msg,sys_login_time,Create_time)values(?,?,?,?,?,?,?,?,?)",
                    param:[sys_loginname,ip,address,browser,OS,0,"验证码错误",new Date(),new Date()]
                };

                startTrans.query(obj.sql,obj.param);
                startTrans.commit();
                callback(null,{code:2,msg:"验证码验证失败"});
                return;
            }
        }catch (e){
            startTrans.rollback();
            callback(e);
            return;
        }
    };

    /**
     * 退出登录
     * @param sessionId
     * @param callback
     * @returns {Promise<void>}
     * @constructor
     */
    static async LogoutService(sessionId,callback){
        let startTrans = null;

        try{
            let connect = await MYSQL().connect();
            startTrans = await connect.startTrans();


            let obj = {
                sql:"delete FROM rl_sys_user_online where sys_user_sessionid = \""+sessionId+"\"",
            }

            let online_row = await startTrans.query(obj.sql);
            await startTrans.commit();

            callback(null,online_row.affectedRows);
            return ;

        }catch (e){
            await startTrans.rollback();
            callback(e);
            return ;
        }
    }
}